package com.airsystem.pos.bengkel.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.airsystem.pos.bengkel.entity.DetailKendaraan;
import com.airsystem.pos.bengkel.entity.Kendaraan;
import com.airsystem.pos.bengkel.util.HibernateUtil;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */
public class KendaraanServices {

	/* GENERIC DAO */
	public void saveOrUpdate(Kendaraan kendaraan) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.saveOrUpdate(kendaraan);
		session.flush();
		session.close();
	}

	public void saveOrUpdate(DetailKendaraan detailKendaraan) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.saveOrUpdate(detailKendaraan);
		session.flush();
		session.close();
	}

	public void delete(Kendaraan kendaraan) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.delete(kendaraan);
		session.flush();
		session.close();
	}

	public void delete(DetailKendaraan detailKendaraan) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.delete(detailKendaraan);
		session.flush();
		session.close();
	}

	public List<Kendaraan> select() throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		List<?> criteria = (List<?>) session.createCriteria(Kendaraan.class).list();
		session.close();

		List<Kendaraan> kendaraans = new ArrayList<Kendaraan>();
		for (Object result : criteria) {
			Kendaraan kendaraan = (Kendaraan) result;
			kendaraans.add(kendaraan);
		}

		return kendaraans;
	}

	public List<DetailKendaraan> subselect() throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		List<?> criteria = (List<?>) session.createCriteria(DetailKendaraan.class).list();
		session.close();

		List<DetailKendaraan> detailKendaraans = new ArrayList<DetailKendaraan>();
		for (Object result : criteria) {
			DetailKendaraan detailKendaraan = (DetailKendaraan) result;
			detailKendaraans.add(detailKendaraan);
		}

		return detailKendaraans;
	}

	/* CUSTOM DAO */
	public void update(String oldMerk, String newMerk) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query query = session.createQuery("UPDATE Kendaraan SET merk=:newMerk WHERE merk=:oldMerk");
		query.setParameter("oldMerk", oldMerk);
		query.setParameter("newMerk", newMerk);
		query.executeUpdate();

		session.flush();
		session.close();
	}

	public void subUpdate(String oldSubmerk, String newSubmerk) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query query = session.createQuery("UPDATE DetailKendaraan SET sub_merk=:newSubmerk WHERE sub_merk=:oldSubmerk");
		query.setParameter("oldSubmerk", oldSubmerk);
		query.setParameter("newSubmerk", newSubmerk);
		query.executeUpdate();

		session.flush();
		session.close();
	}

	public List<DetailKendaraan> select(String searchCombo, String searchText) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();

		StringBuffer QUERY = new StringBuffer("FROM DetailKendaraan WHERE ");
		QUERY.append(searchCombo);
		QUERY.append(" LIKE:LIKE ORDER BY kendaraan.merk ASC");

		StringBuffer LIKE = new StringBuffer("%");
		LIKE.append(searchText);
		LIKE.append("%");

		List<?> query = (List<?>) session.createQuery(QUERY.toString()).setString("LIKE", LIKE.toString()).list();
		session.close();

		List<DetailKendaraan> detailKendaraans = new ArrayList<DetailKendaraan>();
		for (Object result : query) {
			DetailKendaraan detailKendaraan = (DetailKendaraan) result;
			detailKendaraans.add(detailKendaraan);
		}

		return detailKendaraans;
	}

	public List<DetailKendaraan> select(String merk) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		List<?> query = (List<?>) session.createQuery("FROM DetailKendaraan WHERE kendaraan.merk=:merk").setString("merk", merk).list();
		session.close();

		List<DetailKendaraan> detailKendaraans = new ArrayList<DetailKendaraan>();
		for (Object result : query) {
			DetailKendaraan detailKendaraan = (DetailKendaraan) result;
			detailKendaraans.add(detailKendaraan);
		}

		return detailKendaraans;
	}
}